home *** CD-ROM | disk | FTP | other *** search
- package asp.wizard;
-
- import com.sun.java.swing.JLabel;
- import com.sun.java.swing.JTable;
- import com.sun.java.swing.table.TableCellRenderer;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Point;
-
- public class PreviewCellRenderer extends JLabel implements TableCellRenderer {
- public static final int MODE_PAINT = 0;
- public static final int MODE_MEASURE = 1;
- private static final int SIDE_MARGIN = 3;
- private static final int VERT_MARGIN = 2;
- private String _currText;
- private int _row;
- private int _column;
- private JTable _table;
- private TextAttribute[] _textAttributes = new TextAttribute[]{new TextAttribute(this), new TextAttribute(this)};
- private int _maxWidth;
- private int _maxHeight;
- private boolean _isPainting = true;
-
- public void paint(Graphics g) {
- Point loc = ((Component)this).getLocation();
- Dimension dim = ((Component)this).getSize();
- TextAttribute attr = null;
- if (this._row == 0) {
- attr = this._textAttributes[0];
- } else {
- attr = this._textAttributes[1];
- }
-
- g.setFont(attr._font);
- g.setColor(attr._color);
- FontMetrics fm = g.getFontMetrics();
- int text_height = fm.getAscent() + fm.getDescent();
- int y = (dim.height - text_height >> 1) + fm.getAscent();
- int width = fm.stringWidth(this._currText);
- if (this._isPainting) {
- g.drawString(this._currText, 3, y);
- if (attr._underline) {
- g.translate(0, 2);
- g.drawLine(3, y, width, y);
- g.translate(0, 0);
- }
- } else {
- if (this._row == 0 && this._column == 0) {
- this._maxHeight = this._maxWidth = 0;
- }
-
- int total_height = text_height + 8;
- if (total_height > this._maxHeight) {
- this._maxHeight = total_height;
- }
-
- int total_width = width + 12;
- if (total_width > this._maxWidth) {
- this._maxWidth = total_width;
- }
- }
-
- }
-
- private final void setTextAttributes(int index, Font font, boolean underline, Color color) {
- this._textAttributes[index]._font = font;
- this._textAttributes[index]._underline = underline;
- this._textAttributes[index]._color = color;
- }
-
- public final void setFirstRowTextAttributes(Font font, boolean underline, Color color) {
- this.setTextAttributes(0, font, underline, color);
- }
-
- public final void setOtherRowTextAttributes(Font font, boolean underline, Color color) {
- this.setTextAttributes(1, font, underline, color);
- }
-
- public Dimension getMaximumCellSize(Dimension placeHolder) {
- if (placeHolder == null) {
- placeHolder = new Dimension();
- }
-
- placeHolder.width = this._maxWidth;
- placeHolder.height = this._maxHeight;
- return placeHolder;
- }
-
- public void setMode(int mode) {
- switch (mode) {
- case 0:
- this._isPainting = true;
- break;
- case 1:
- this._isPainting = false;
- break;
- default:
- this._isPainting = true;
- }
-
- }
-
- public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
- if (value != null) {
- this._currText = value.toString();
- }
-
- this._row = row;
- this._column = column;
- this._table = table;
- return this;
- }
- }
-